using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;

namespace PocoReport
{
    public class TextStyle: Style
    {
        public TextStyle()
        {            
        }

        public TextStyle(XElement element): base(element)
        {
        }

        public Font Font 
        {
            get
            {
                return GetFont(this);
            }
            set 
            {
                SetFont(value);
            }
        }

        public bool PageBreakAfter
        {
            get
            {
                string property = Properties["page-break-after"];
                if(property == null)
                {
                    return false;
                }
                bool breakAfter;
                return bool.TryParse(property, out breakAfter) && breakAfter;
            }
            set
            {
                if(value)
                {
                    Properties["page-break-after"] = "true";
                }
                else
                {
                    Properties.Remove("page-break-after");
                }
            }
        }

        private void SetFont(Font value)
        {
            Properties.Remove("font-style");
            Properties.Remove("font-size");
            Properties.Remove("font-name");

            if (value != null)
            {
                List<string> fontStyle = new List<string>();
                if (value.Bold) fontStyle.Add("bold");
                if (value.Italic) fontStyle.Add("italic");
                if (value.Underline) fontStyle.Add("underline");
                if (value.Strikeout) fontStyle.Add("strikeout");
                if (fontStyle.Count > 0)
                {
                    Properties["font-style"] = string.Join(" ", fontStyle.ToArray());
                }

                if (value.Size != DefaultFont.Size)
                {
                    Properties["font-size"] = value.Size.ToString();
                }

                if (value.Name != DefaultFont.Name)
                {
                    Properties["font-name"] = value.Name;
                }
            }
        }

        private static readonly Font DefaultFont = new Font(FontFamily.GenericSansSerif, 10);
        public static Font GetFont(Style style)
        {
            string property = style.Properties["font-style"];
            FontStyle s = DefaultFont.Style;
            if (property != null)
            {
                string[] fontStyle = property.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                if (fontStyle.Contains("bold"))
                {
                    s |= FontStyle.Bold;
                }
                if (fontStyle.Contains("underline"))
                {
                    s |= FontStyle.Underline;
                }
                if (fontStyle.Contains("italic"))
                {
                    s |= FontStyle.Italic;
                }
                if (fontStyle.Contains("strikeout"))
                {
                    s |= FontStyle.Strikeout;
                }
            }

            property = style.Properties["font-size"];
            float fontSize = DefaultFont.Size;
            if (property != null)
            {
                fontSize = float.Parse(property);
            }

            property = style.Properties["font-name"];
            string fontName = DefaultFont.Name;
            if (property != null)
            {
                fontName = property;
            }

            Font font = new Font(fontName, fontSize, s);
            return font;
        }
    }
}